#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<unistd.h>
#include<sys/types.h>
#include<wait.h>

using namespace std;

int basesize = 1024;

string GetUserName()
{
    string username = getenv("USER");
    return username.empty() ? "None" : username;
}

string GetHostName()
{
    string hostname = getenv("HOSTNAME");
    return hostname.empty() ? "None" : hostname;
}


string GetPWD()
{
    string pwd = getenv("PWD");
    return pwd.empty() ? "None" : pwd;
}

string MakeCommandLine()
{
    char command_line[basesize];
    snprintf(command_line, basesize, "[%s@%s %s]#", \
            GetUserName().c_str(), GetHostName().c_str(), GetPWD().c_str());
    return command_line;
}

void PrintfCommandLine()
{
    printf("%s", MakeCommandLine().c_str());
    fflush(stdout);
}

bool GetCommandLine(char command_buffer[], int size)    // 2. 获取用户命令
{                                     
    char *result = fgets(command_buffer, size, stdin);
    if(!result)                       
    {                                 
        return false;                 
    }                                 
    command_buffer[strlen(command_buffer)-1] = 0; // 把最后输入的回车字符改为0，即纯净版输入                                    
    if(strlen(command_buffer) == 0) return false;
    return true;                      
}

int main()
{
    while(true)
    {
        PrintfCommandLine();// 1.命令行提示符
        //printf("\n");
        //sleep(1);
        //
        if(!GetCommandLine(command_buffer, basesize))    // 2. 获取用户命令
          { 
             // 依次获取每个子字符串                           
             continue; // 获取到空格继续                
          }                            
          printf("%s\n",command_buffer);
        //AnalysisCommandLine();//3.分析命令
        //ExecuteCommand(); // 4.执行命
    }

    return 0;
}
